#include "ScreenshotConfig.h"
#include <QDir>
#include <QStandardPaths>

ScreenshotConfig::ScreenshotConfig()
    : m_defaultSavePath(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation))
    , m_defaultFileName("Screenshot_%1")
    , m_defaultFormat("png")
    , m_jpegQuality(90)
    , m_autoCopyToClipboard(true)
    , m_autoSave(false)
{
}

QJsonObject ScreenshotConfig::toJson() const
{
    QJsonObject json;
    json["defaultSavePath"] = m_defaultSavePath;
    json["defaultFileName"] = m_defaultFileName;
    json["defaultFormat"] = m_defaultFormat;
    json["jpegQuality"] = m_jpegQuality;
    json["autoCopyToClipboard"] = m_autoCopyToClipboard;
    json["autoSave"] = m_autoSave;
    return json;
}

ScreenshotConfig ScreenshotConfig::fromJson(const QJsonObject& json)
{
    ScreenshotConfig config;
    
    if (json.contains("defaultSavePath")) {
        config.m_defaultSavePath = json["defaultSavePath"].toString();
    }
    if (json.contains("defaultFileName")) {
        config.m_defaultFileName = json["defaultFileName"].toString();
    }
    if (json.contains("defaultFormat")) {
        config.m_defaultFormat = json["defaultFormat"].toString();
    }
    if (json.contains("jpegQuality")) {
        config.m_jpegQuality = json["jpegQuality"].toInt();
    }
    if (json.contains("autoCopyToClipboard")) {
        config.m_autoCopyToClipboard = json["autoCopyToClipboard"].toBool();
    }
    if (json.contains("autoSave")) {
        config.m_autoSave = json["autoSave"].toBool();
    }
    
    return config;
}

bool ScreenshotConfig::isValid() const
{
    // 检查保存路径是否存在或可创建
    if (!m_defaultSavePath.isEmpty()) {
        QDir dir(m_defaultSavePath);
        if (!dir.exists() && !dir.mkpath(m_defaultSavePath)) {
            return false;
        }
    }
    
    // 检查格式是否有效
    QString format = m_defaultFormat.toLower();
    if (format != "png" && format != "jpg" && format != "jpeg") {
        return false;
    }
    
    // 检查 JPEG 质量范围
    if (m_jpegQuality < 0 || m_jpegQuality > 100) {
        return false;
    }
    
    return true;
}



